package com.zh.baselibrary.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.zh.baselibrary.constant.ExtraName;


public class IntentUtils {

    private static volatile IntentUtils mIntentUtils = null;

    public static IntentUtils instance() {
        if (mIntentUtils == null) {
            synchronized (IntentUtils.class) {
                if (mIntentUtils == null)
                    mIntentUtils = new IntentUtils();
            }
        }
        return mIntentUtils;
    }

    /**
     * 不带参数的跳转
     *
     * @param fromClass 上下文
     * @param toClass   泛型
     */
    public void startActivity(Context fromClass, Class<? extends AppCompatActivity> toClass) {
        Intent intent = new Intent(fromClass, toClass);
        fromClass.startActivity(intent);
    }

    /**
     * 带参数的跳转
     *
     * @param fromClass 上下文
     * @param toClass   泛型
     */
    public void startActivity(Context fromClass, Class<? extends AppCompatActivity> toClass, Bundle bundle) {
        Intent intent = new Intent(fromClass, toClass);
        intent.putExtras(bundle);
        fromClass.startActivity(intent);
    }
    /**
     * 不带参数的跳转 并关闭当前ACTIVITY
     *
     * @param fromClass activity
     * @param toClass   泛型 必须继承activity
     */
    public void startActivityFinish(AppCompatActivity fromClass, Class<? extends AppCompatActivity> toClass) {
        Intent intent = new Intent(fromClass, toClass);
        fromClass.startActivity(intent);
        fromClass.finish();
    }
    public void startActivityFinish(Context fromClass, Class<? extends AppCompatActivity> toClass) {
        Intent intent = new Intent(fromClass, toClass);
        fromClass.startActivity(intent);
        ((Activity)fromClass).finish();
    }
    /**
     * 带参数的跳转 并关闭当前ACTIVITY
     *
     * @param fromClass activity
     * @param toClass   泛型 必须继承activity
     */
    public void startActivityFinish(AppCompatActivity fromClass, Class<? extends AppCompatActivity> toClass, Bundle bundle) {
        Intent intent = new Intent(fromClass, toClass);
        intent.putExtras(bundle);
        fromClass.startActivity(intent);
        fromClass.finish();
    }

    /**
     * 带参数跳转返回
     *
     * @param fromClass   activity
     * @param toClass     泛型 必须继承activity
     * @param requestCode 请求码
     * @param bundle      传递参数
     */
    public void startActivityForResult(AppCompatActivity fromClass, Class<? extends AppCompatActivity> toClass, int requestCode, Bundle bundle) {
        Intent intent = new Intent(fromClass, toClass);
        if (null != bundle)
            intent.putExtras(bundle);
        fromClass.startActivityForResult(intent, requestCode);
    }

    /**
     * 有无带参数返回上一个页面 关闭当前页
     *
     * @param fromClass  activity
     * @param resultCode 返回码
     * @param bundle     传递参数
     */
    public void setResult(AppCompatActivity fromClass, int resultCode, Bundle bundle) {
        Intent intent = new Intent();
        if (null != bundle)
            intent.putExtras(bundle);
        fromClass.setResult(resultCode, intent);
        fromClass.finish();
    }

}
